package com.jhy.szl.daima20240817;

import java.util.HashMap;
import java.util.Map;

/**
 * 两数之和
 */
public class TwoSum {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int num = target - nums[i];
            if (hashMap.containsKey(num)) {
                return new int[]{i, hashMap.get(num)};
            } else {
                hashMap.put(nums[i], i);
            }
        }
        return new int[]{};
    }

}
